package groups.diseases.daobak;

import java.sql.*;
import java.util.Vector;

import groups.diseases.*;

public class JDBCHumanSymptomDAO implements IHumanSymptomDAO{
	private Connection connection; 
	
	public JDBCHumanSymptomDAO(Connection con){
		connection = con;
	}
	
	public HumanSymptom create(String description)	throws SQLException {
		Statement stm = connection.createStatement();
		
		ResultSet res = stm.executeQuery("select max(Id) from HumanSymptom");
		
		int id;
		if(res.next())
			id = res.getInt(1)+1;
		else
			id = 0;
		
		stm.executeUpdate("insert into HumanSymptom (id,description) values("+id+"," +
				"'"+description+"')");

		stm.close();
				
		return new HumanSymptom(id, description);
	}

	public void update(HumanSymptom symp) throws SQLException {
		Statement stm = connection.createStatement();
		
		stm.executeUpdate("update HumanSymptom set " +
				"description='"+symp.getDescription()+"'"+
				" where id="+symp.getId());
		
	}

	public void delete(HumanSymptom symp) throws SQLException {
		Statement stm = connection.createStatement();
		
		stm.executeUpdate("delete from HumanSymptom where id="+symp.getId());
		
	}

	public Vector<HumanSymptom> getList() throws SQLException {
		Statement stm = connection.createStatement();
		ResultSet res = stm.executeQuery("select * from HumanSymptom");
		
		Vector<HumanSymptom> vec = new Vector<HumanSymptom>();
		
		while(res.next())
			vec.add(new HumanSymptom(res.getInt(1),res.getString(2)));
		
		return vec;
	}

	public HumanSymptom getItem(int id) throws SQLException {
		Statement stm = connection.createStatement();
		
		ResultSet res = stm.executeQuery("select * from HumanSymptom where id="+id);
		
		if(res.next())
			return new HumanSymptom(res.getInt(1),res.getString(2));
		else
			return null;
	}

}
